#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/2/18 14:34
@desc
"""
from concurrent.futures import ThreadPoolExecutor

from countdown import CountDownLatch
from multithread.subProcess import MyClass


def func(n, cdl):
    print('func-' + str(n))
    # import time
    # time.sleep(15)
    # print(n)
    my = MyClass()
    stdout, stderr = my.run_no_cdl(n)
    print('func-' + str(n) + "-down")
    # tpool.submit(my.run, n, cdl)
    cdl.countDown()
    return n * n, stdout, stderr


def call_back(m):
    # print(m.__dict__)
    print('结果是:%s' % (str(m.result())))
    # print(str(m.result()))

if __name__ == '__main__':
    cdl = CountDownLatch(2)
    tpool = ThreadPoolExecutor(max_workers=5, thread_name_prefix='sub-task-')  # 默认 不要超过cpu个数*5


    rets = []
    for i in range(2):
        # t = tpool.submit(my.run, i, cdl)
        t = tpool.submit(func, i, cdl)
        rets.append(t)

    print('设置阻塞...')
    cdl.await()

    print('获取结果...')
    for t in rets:
        call_back(t)


